package com.apes.scm.statement.customerConsignStatement.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.Arith;
import com.apes.scm.account.account.model.Cooperation;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.*;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CustomerConsignStatement.CONST_TABLE_NAME,
        indexes = {
                @Index(name = "idx_statement_customer_id", columnList = "customer_id"),
                @Index(name = "idx_statement_cooperation_id", columnList = "cooperation_id")
        })
public class CustomerConsignStatement extends Model {
    public static final String CONST_TABLE_NAME = "client_consign_statement";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "客户寄售对账单", prefix = "C")
    @Comment("对账单号")
    private String id;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司编码")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    @Comment("部门编码")
    private Dept dept;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_id", referencedColumnName = "id")
    @Comment("客户编码")
    private PartyRole customer;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cooperation_id", referencedColumnName = "id", nullable = false)
    @Comment("合作方式编码")
    private Cooperation cooperation;


    @Column(name = "amt_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("对账金额")
    private double amtTotal = 0;


    @Column(name = "is_return", columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE, nullable = false)
    @Comment("是否退回")
    private boolean returned = false;

    @States(name = "单据状态", values = {
            @State(id = "sent", name = "新增"),
            @State(id = "approve", name = "审核")})
    @Column(name = "state", length = 10, nullable = false)
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUID;

    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note; //备注

    // mapping 入口
    @Transient
    private List<CustomerConsignStatementItem> items;


    @JSONField(serialize = false)
    @OneToMany(mappedBy = "customerConsignStatement", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<CustomerConsignStatementItem> customerConsignStatementItems = new ArrayList<>();


    public List<CustomerConsignStatementItem> getCustomerConsignStatementItems() {
        if(items != null) customerConsignStatementItems = items;
        for (CustomerConsignStatementItem item : customerConsignStatementItems) {
            item.setCustomerConsignStatement(this);
        }
        return customerConsignStatementItems;
    }


    /*
     * 计算总和
     * */
    public void computeAmtAndSetItemsId() {
        this.amtTotal = 0;
        for(int i = 0, len = customerConsignStatementItems.size(); i < len; i++) {
            CustomerConsignStatementItem item = customerConsignStatementItems.get(i);
            item.setId(id + "-" + i);
            item.computeAmt();
            this.amtTotal += item.getAmount();
        }
    }


}
